<!DOCTYPE html>
<html>
  <!-- Html Head Tag-->
  <head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="">
  <meta name="author" content="cityking">
  <!-- Open Graph Data -->
  <meta property="og:title" content="python字符串操作总结"/>
  <meta property="og:description" content="" />
  <meta property="og:site_name" content="python &amp;&amp; django"/>
  <meta property="og:type" content="article" />
  <meta property="og:image" content="https://cityking.github.io/hexo_project"/>
  
    <link rel="alternate" href="/atom.xml" title="python &amp;&amp; django" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  

  <!-- Site Title -->
  <title>python && django</title>

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="/hexo_project/css/bootstrap.min.css">
  <!-- Custom CSS -->
  
  <link rel="stylesheet" href="/hexo_project/css/style.light.css">

  <!-- Google Analytics -->
  

</head>

  <body>
    <!-- Page Header -->


<header class="site-header header-background" style="background-image: url(/hexo_project//img/background.jpg)">
  <div class="container">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        <div class="page-title with-background-image">
          <p class="title">python字符串操作总结</p>
          <p class="subtitle"></p>
        </div>
        <div class="site-menu with-background-image">
          <ul>
            
              <li>
                <a href="/hexo_project/">
                  
                  Home
                  
                </a>
              </li>
            
              <li>
                <a href="/hexo_project/archives">
                  
                  Archives
                  
                </a>
              </li>
            
              <li>
                <a href="https://github.com/cityking">
                  
                  Github
                  
                </a>
              </li>
            
              <li>
                <a href="mailto:<154443603@qq.com>">
                  
                  Email
                  
                </a>
              </li>
            
          </ul>
        </div>
      </div>
    </div>
  </div>
</header>

<article>
  <div class="container typo">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        <div class="post-info text-muted">
          
            <!-- Author -->
            <span class="author info">By cityking</span>
          
          <!-- Date -->
          <span class="date-time info">On
            <span class="date">2018-03-08</span>
            <span class="time">18:03:36</span>
          </span>
          
          <!--  Categories  -->
            <span class="categories info">Under 

<a href="/hexo_project/categories/python/">python</a>
</span>
          
        </div>
        <!-- Tags -->
        
          <div class="post-tags text-muted">
            Tags: 

<a class="tag" href="/hexo_project/tags/python/">#python</a> <a class="tag" href="/hexo_project/tags/字符串/">#字符串</a> <a class="tag" href="/hexo_project/tags/切片/">#切片</a> <a class="tag" href="/hexo_project/tags/format/">#format</a>


          </div>
        
        <!-- Post Main Content -->
        <div class="post-content">
          <p>Python中的字符串操作<br><a id="more"></a></p>
<h1 id="常用方法"><a href="#常用方法" class="headerlink" title="常用方法"></a>常用方法</h1><p>1.切片操作：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">str1=&quot;hello world!&quot; </span><br><span class="line">str1[1:3] &lt;=&gt; &apos;el&apos;(左闭右开：即是从1到2)</span><br><span class="line">str[:3] &lt;=&gt; &apos;hel&apos;</span><br><span class="line">str[2:] &lt;=&gt; &apos;llo world!&apos;</span><br></pre></td></tr></table></figure></p>
<p>2.capitalize():将字符串第一个字符大写<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&apos;hello world!&apos;</span><br><span class="line">&gt;&gt;&gt; str.capitalize ()</span><br><span class="line">&apos;Hello world!&apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>3.casefold（）：将整个字符串小写<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str1=&quot;Hello world!&quot;</span><br><span class="line">&gt;&gt;&gt; str1.casefold ()</span><br><span class="line">&apos;hello world!&apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>4.center(width)：将整个字符串居中（如果不够width则用空格补充）<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">str1=&quot;Hello world!&quot;</span><br><span class="line">&gt;&gt;&gt; str1.center(20)</span><br><span class="line">&apos; Hello world! &apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>5.count（sub[,start[,end]]）:sub从start到end出现的次数（默认是整个字符串）<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">str1=&quot;Hello world!&quot;</span><br><span class="line">&gt;&gt;&gt; str1.count (&apos;l&apos;,3)</span><br><span class="line">2（&quot;Hello world!&quot;）</span><br><span class="line">&gt;&gt;&gt; str1.count (&apos;l&apos;)</span><br><span class="line">3（&quot;Hello world!&quot;）</span><br><span class="line">&gt;&gt;&gt; str1.count(&apos;l&apos;,3,6)</span><br><span class="line">1（&quot;Hello world!&quot;）</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>6.endswith（sub）判断是否是以哪个字符串结尾<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">str1=&quot;Hello world!&quot;</span><br><span class="line">&gt;&gt;&gt; str1.endswith(&apos;orld!&apos;)</span><br><span class="line">True（&quot;Hello world!&quot;）</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>7.expandtabs（）:将字符串中的’\t’转换为空格<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str2=&apos;include world!&apos;</span><br><span class="line">&gt;&gt;&gt; str2.expandtabs()</span><br><span class="line">&apos;include world!&apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>8.find（sub[,start][,end]）:查找字符串中子串从start到end出现的位置并返回下标<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">str1=&quot;Hello world!&quot;</span><br><span class="line">&gt;&gt;&gt; str1.find(&apos;llo&apos;)</span><br><span class="line">2（&quot;Hello world!&quot;）</span><br><span class="line">&gt;&gt;&gt; str1.find(&apos;llo&apos;,3,8)</span><br><span class="line">-1</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>9.isalnum（）:判断s是否是数字或者字母<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">str1=&quot;Hello world!&quot;</span><br><span class="line">&gt;&gt;&gt; str1.isalnum()</span><br><span class="line">False（&quot;Hello world!&quot;）</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>10.isspace（）：判断是否是空格<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&quot; &quot;</span><br><span class="line">&gt;&gt;&gt; str.isspace()</span><br><span class="line">True</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>11.isdigit（）：判断是否都是数字组成<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&quot;12345dfgbhn&quot;</span><br><span class="line">&gt;&gt;&gt; str.isdigit()</span><br><span class="line">False（&quot;12345dfgbhn&quot;）</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>12.isalpha（）：判断是否都是由字母组成的<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&apos;asdfghj&apos;</span><br><span class="line">&gt;&gt;&gt; str.isalpha()</span><br><span class="line">True</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>13.islower（）：判断是否都是由小写字母组成的<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&apos;asdfghj&apos;</span><br><span class="line">&gt;&gt;&gt; str.islower()</span><br><span class="line">True</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>14.istitle（）：判断是否是标题形式字符串（即是连续字符串只有第一个字母大写，其他都是小写，若是有空格，则每个分隔的字符串都满足此）<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&apos;Helloworld&apos;</span><br><span class="line">&gt;&gt;&gt; str.istitle()</span><br><span class="line">True</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>15.isupper（）：判断是否都是由大写字母组成的<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&apos;HELLO WOLD&apos;</span><br><span class="line">&gt;&gt;&gt; str.isupper()</span><br><span class="line">True</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>16.join（sub）<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str1=&quot;abc&quot;</span><br><span class="line">&gt;&gt;&gt; str1.join(&apos;1234&apos;)</span><br><span class="line">&apos;1abc2abc3abc4&apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>17.lstrip（）：去掉字符串左边所有空格<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&quot; hello world!&quot;</span><br><span class="line">&gt;&gt;&gt; str.lstrip()</span><br><span class="line">&apos;hello world!&apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>18.rstrip（）：去掉字符串右边的空格<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&quot;hello world! &quot;</span><br><span class="line">&gt;&gt;&gt; str.rstrip()</span><br><span class="line">&apos;hello world!&apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>19.replace（old,[,new][,count]）:将字符串中的old子串替换为new，替换count次<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">str=&apos;hello world!&apos;</span><br><span class="line">&gt;&gt;&gt; str.replace(&apos;hello&apos; ,&apos;HELLO&apos; ,2)</span><br><span class="line">&apos;HELLO world! &apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>20.rfind（sub[,start][,end]）:从右边开始查找字符串中子串从start到end出现的位置并返回下标（注意start和end是从左往右的，返回的也是从左到右的位置。）<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&quot;hello world!&quot;</span><br><span class="line">&gt;&gt;&gt; str.rfind(&apos;d!&apos;,0,5)</span><br><span class="line">-1</span><br><span class="line">&gt;&gt;&gt; str.rfind(&apos;d!&apos;)</span><br><span class="line">10</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>21.split（sep）：将字符串用给定的标准分割，并且以列表形式返回分割后的元素组<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&quot;1,2,3,4&quot;</span><br><span class="line">&gt;&gt;&gt; str.split(&apos;,&apos;)</span><br><span class="line">[&apos;1&apos;, &apos;2&apos;, &apos;3&apos;, &apos;4&apos;]</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>22.startwith（sub[,start][,end]）:判断从start到end是否以sub开头<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str.startswith(&apos;hel&apos;)</span><br><span class="line">True</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>23.strip（）：去掉字符串左右两边的空格<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&apos; hello world! &apos;</span><br><span class="line">&gt;&gt;&gt; str.strip()</span><br><span class="line">&apos;hello world!&apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>24.swapcase（）：将字符串的大小写反转<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&quot;Hello world!&quot;</span><br><span class="line">&gt;&gt;&gt; str.swapcase ()</span><br><span class="line">&apos;hELLO WORLD!&apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>25.title（）将字符串标题化（即是连续字符串的第一个字母大写，其他都是小写空格，分隔的字符串都遵循此规则）<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&quot;hello world!&quot;</span><br><span class="line">&gt;&gt;&gt; str.title()</span><br><span class="line">&apos;Hello World!&apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>26.translate（table）<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&quot;sssaabb&quot;</span><br><span class="line">&gt;&gt;&gt; str.translate(str.maketrans(&apos;s&apos;,&apos;b&apos;))</span><br><span class="line">&apos;bbbaabb&apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>27.upper（）：将整个字符串都大写<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&quot;hello world!&quot;</span><br><span class="line">&gt;&gt;&gt; str.upper()</span><br><span class="line">&apos;HELLO WORLD!&apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>28.zfill（width）：用’0’来填充不够的空格（是从左边开始填充）<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&quot;hello world! &quot;</span><br><span class="line">&gt;&gt;&gt; str.zfill(20)</span><br><span class="line">&apos;00000hello world! &apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>29.lower（）：将整个字符串都小写<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; str=&quot;HELLO worldQ&quot;</span><br><span class="line">&gt;&gt;&gt; str.lower()</span><br><span class="line">&apos;hello worldq&apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<h1 id="格式化："><a href="#格式化：" class="headerlink" title="格式化："></a>格式化：</h1><h2 id="1-format（）"><a href="#1-format（）" class="headerlink" title="1.format（）"></a>1.format（）</h2><h3 id="“映射”示例"><a href="#“映射”示例" class="headerlink" title="“映射”示例"></a>“映射”示例</h3><p>通过位置映射<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; &apos;&#123;0&#125; love &#123;1&#125;&#123;2&#125;&apos;.format(&apos;I&apos;,&apos;my&apos;,&apos;home&apos;)</span><br><span class="line">&apos;I love myhome&apos;</span><br><span class="line">&gt;&gt;&gt; &apos;&#123;0&#125; love &#123;1&#125; &#123;2&#125;&apos;.format(&apos;I&apos;,&apos;my&apos;,&apos;home&apos;)</span><br><span class="line">&apos;I love my home&apos;</span><br></pre></td></tr></table></figure></p>
<p>通过关键字参数映射<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; &apos;&#123;a&#125; love &#123;b&#125; &#123;c&#125;&apos;.format(a=&apos;I&apos;,b=&apos;my&apos;,c=&apos;home&apos;)</span><br><span class="line">&apos;I love my home&apos;</span><br></pre></td></tr></table></figure></p>
<p>通过对象属性<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">class Person:</span><br><span class="line">    def __init__(self, name, age):</span><br><span class="line">        self.name, self.age = name, age</span><br><span class="line">    def __str__(self):</span><br><span class="line">        return &quot;&#123;self.name&#125; , &#123;self.age&#125;&quot;.format(self=self)</span><br></pre></td></tr></table></figure></p>
<p>通过下标<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">l = [&quot;wangcheng&quot;,&quot;23&quot;,]</span><br><span class="line">s3 = &quot;&#123;0[0]&#125;, &#123;0[1]&#125;&quot;.format(l)</span><br></pre></td></tr></table></figure></p>
<h3 id="格式限定符"><a href="#格式限定符" class="headerlink" title="格式限定符"></a>格式限定符</h3><p>填充和对齐<br>^、&lt;、&gt;分别是居中、左对齐、右对齐，后面带宽度<br>:号后面带填充的字符，只能是一个字符，不指定的话默认是用空格填充<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">s4 = &quot;&#123;:&gt;8&#125;&quot;.format(189)</span><br><span class="line">s4 = &quot;&#123;:#&lt;8&#125;&quot;.format(189)</span><br><span class="line">s4 = &quot;&#123;:#^8&#125;&quot;.format(189)</span><br></pre></td></tr></table></figure></p>
<p>精度与类型f<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">s5 = &quot;&#123;:.2f&#125;&quot;.format(233.232556)</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; &apos;&#123;0:.1f&#125;&#123;1&#125;&apos;.format(27.658,&apos;GB&apos;)</span><br><span class="line">&apos;27.7GB&apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure>
<p>其中.2表示长度为2的精度，f表示float类型</p>
<p>进制转换<br>b、d、o、x分别是二进制、十进制、八进制、十六进制<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">s6 = &quot;&#123;:b&#125;&quot;.format(17)</span><br><span class="line">s6 = &quot;&#123;:d&#125;&quot;.format(17)</span><br><span class="line">s6 = &quot;&#123;:o&#125;&quot;.format(17)</span><br><span class="line">s6 = &quot;&#123;:x&#125;&quot;.format(17)</span><br></pre></td></tr></table></figure></p>
<p>千位分隔符,<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">s6 = &quot;&#123;:,&#125;&quot;.format(1234567890)</span><br><span class="line">1,234,567,890</span><br></pre></td></tr></table></figure></p>
<h2 id="2-百分号"><a href="#2-百分号" class="headerlink" title="2.百分号%"></a>2.百分号%</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; &quot;%d+%d=%d&quot; % (4,5,4+5)</span><br><span class="line">&apos;4+5=9&apos;</span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line"></span><br><span class="line">&gt;&gt;&gt; &apos;%c&apos; % 97</span><br><span class="line">&apos;a&apos;</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure>

        </div>
      </div>
    </div>
  </div>
</article>



    <!-- Footer -->
<footer>
  <div class="container">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        <p class="copyright text-muted">
          Theme By <a target="_blank" href="https://github.com/levblanc">Levblanc.</a>
          Inspired By <a target="_blank" href="https://github.com/klugjo/hexo-theme-clean-blog">Clean Blog.</a>
        <p class="copyright text-muted">
          Powered By <a target="_blank" href="https://hexo.io/">Hexo.</a>
        </p>
      </div>
    </div>
  </div>
</footer>


    <!-- After Footer Scripts -->
<script src="/hexo_project/js/highlight.pack.js"></script>
<script>
  document.addEventListener("DOMContentLoaded", function(event) {
    var codeBlocks = Array.prototype.slice.call(document.getElementsByTagName('pre'))
    codeBlocks.forEach(function(block, index) {
      hljs.highlightBlock(block);
    });
  });
</script>

  </body>
</html>

